package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车接口
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //拿到当前用户id
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(userId);
        //判断在购物车是否已存在
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        //存在就数量加一
        if (ObjectUtils.isNotEmpty(list)){
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.updateNum(cart);
        }else {
            //不存在就根据id搜索获取其他参数,封装后新增
            if (ObjectUtils.isNotEmpty(shoppingCartDTO.getDishId())){
                DishVO dishVO = dishMapper.selectById(shoppingCartDTO.getDishId());
                shoppingCart.setImage(dishVO.getImage());
                shoppingCart.setAmount(dishVO.getPrice());
                shoppingCart.setName(dishVO.getName());
            }else if (ObjectUtils.isNotEmpty(shoppingCartDTO.getSetmealId())){
                SetmealVO setmealVO = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setImage(setmealVO.getImage());
                shoppingCart.setAmount(setmealVO.getPrice());
                shoppingCart.setName(setmealVO.getName());
            }
            shoppingCart.setNumber(1);
            shoppingCartMapper.addShoppingCart(shoppingCart);
        }
    }

    /**
     * 查询购物车接口
     */
    @Override
    public List<ShoppingCart> queryShoppingCart() {
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(BaseContext.getCurrentId()).build();
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }

    /**
     * 清空购物车接口
     */
    @Override
    public void cleanShoppingCart() {
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(BaseContext.getCurrentId()).build();
        shoppingCartMapper.cleanShoppingCart(shoppingCart);
    }

    /**
     * 删除购物车接口
     */
    @Override
    public void deleteShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        ShoppingCart cart = list.get(0);

        if (cart.getNumber() == 1){
            shoppingCartMapper.deleteShoppingCart(cart.getId());
        }else {
            cart.setNumber(cart.getNumber() - 1);
            shoppingCartMapper.updateNum(cart);
        }
    }
}
